package com.zhisuo.apicenter.service.imp;


import com.zhisuo.apicenter.model.*;
import com.zhisuo.apicenter.service.ExpressManService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ExpressManServiceImp extends BaseServiceImp implements ExpressManService {


    @Override
    public ZhiSuoCar getCarBySn(String sn) {
        String sql="select * from zhisuo_car where sn=? and status=1";
        return getEntry(sql,new ZhiSuoCarRowMapper(),new Object[]{sn});
    }

    @Override
    public String getParkareaidByCarId(int id) {
        String sql="select parkareaid from parkarea_car where carid=?";
        return queryString(sql,new Object[]{id});
    }

    @Override
    public String getParkAreaNameById(String parkareaid) {
        String sql="select name from parkarea_info where id=?";
        return queryString(sql,new Object[]{parkareaid});
    }



    @Override
    public ZhiSuoCarBox getBoxIdByBoxName(String boxname) {
        String sql="select * from zhisuo_car_box where name=? and isempty=0 and status=1 limit 1";
        return getEntry(sql,new ZhiSuoCarBoxRowMapper(),new Object[]{boxname});
    }

    @Override
    public void insertNewDeliveryOrderByUserId(String carid, int boxid, String boxcode, String paymentcode, String receiptusername, String userid,String paymentcompany,String receiptusermobile) {
        String sql="insert into box_delivery_order(carid,boxid,boxcode,paymentcode,paymentcompany,receiptusername,create_time,userid,status,receiptusermobile,ordertype) values(?,?,?,?,?,?,?,?,?,?,?)";
        update(sql,new Object[]{carid,boxid,boxcode,paymentcode,paymentcompany,receiptusername,new Date(),userid,0,receiptusermobile,0});
    }

    @Override
    public String getSalemansIdByUserId(String userid) {
        String sql="select salemansid from zhisuo_user where id=?";
        return queryString(sql,new Object[]{userid});
    }

    @Override
    public String getPayMentCompanyById(String salemansid) {
        String sql="select companyname from zhisuo_user_salesman where id=?";
        return queryString(sql,new Object[]{salemansid});
    }

    @Override
    public List<DeliveryOrder> getListDeliveryOrderByUserIdAndStatus(String userid, String status,Integer page,Integer pagesize) {
        //0已装车  1已取走
        //0待派件   1待派件系统分配  2待派件时间预约 3已签收
        String sql="";
        if(status.equals("0")){
            sql= "select * from box_delivery_order where userid=? and status in(0,1,2) and ordertype=0  order by create_time desc limit ?,?";
        }else{
            sql= "select * from box_delivery_order where userid=? and status in(3) and ordertype=0 order by create_time desc limit ?,?";
        }

        return getEntryList(sql,new DeliveryOrderRowMapper(),new Object[]{userid, (page - 1) * pagesize, pagesize});
    }

    @Override
    public ZhiSuoCar getCarByCarId(Integer carid) {
        String sql="select * from zhisuo_car where id=?";
        return getEntry(sql,new ZhiSuoCarRowMapper(),new Object[]{carid});
    }

    @Override
    public List<ZhiSuoCarBox> getListZhiSuoCarBoxByCarId(String carid) {
        String sql="select * from zhisuo_car_box where carid=?";
        return getEntryList(sql,new ZhiSuoCarBoxRowMapper(),new Object[]{carid});
    }

    @Override
    public ZhiSuoCarStatus getCarStatusByCarVin(String sn) {
        String sql="select * from zhisuo_car_status where vin=? order by update_time desc limit 1";
        return getEntry(sql,new ZhiSuoCarStatusRowMapper(),new Object[]{sn});
    }

    @Override
    public void updateCarStopListStatus(int carid, String stop) {
        //0未到站，1已到站
        String sql="update zhisuo_car_nextstoplist set status=1 where carid=? and nextstop=?";
        update(sql,new Object[]{carid,stop});
    }

    @Override
    public ZhiSuoCar getCarByQrcode(String sn) {
        String sql="select * from zhisuo_car where qrcode=? and status=1";
        return getEntry(sql,new ZhiSuoCarRowMapper(),new Object[]{sn});
    }

    @Override
    public DeliveryOrder getNextDeliveryOrderByNextStop(String nextstop, String carid) {
        String sql="select * from box_delivery_order where carid=? and receiptuseraddresscode=? and status in(1,2)";
        return getEntry(sql,new DeliveryOrderRowMapper(),new Object[]{carid,nextstop});
    }

    @Override
    public String getStopTcpStatusByCarIdAndStop(int id, String stop) {
        System.out.println("select stoptcp from zhisuo_car_nextstoplist where carid="+id+" and nextstop="+stop+" and status=0");
        String sql="select stoptcp from zhisuo_car_nextstoplist where carid=? and nextstop=? and status=0";
        return queryString(sql,new Object[]{id,stop});
    }

    @Override
    public void updateCarStopTcpStatus(int carid, String stop) {
        String sql="update zhisuo_car_nextstoplist set stoptcp=1 where carid=? and nextstop=?";
        update(sql,new Object[]{carid,stop});
    }

    @Override
    public void updateCarBoxIsEmpty(String boxname, String carid) {
        String sql="update zhisuo_car_box set isempty=1,isorder=1 where name=? and carid=?";
        update(sql,new Object[]{boxname,carid});

    }

    @Override
    public void updateCarBoxStatusByCode(String boxcode,String carid) {
        String sql="update zhisuo_car_box set isempty=0,isorder=0 where code=? and carid=?";
        update(sql,new Object[]{boxcode,carid});
    }

    @Override
    public List<DeliveryOrder> getListNextDeliveryOrderByNextStop(String nextstop, String carid) {
        String sql="select * from box_delivery_order where carid=? and receiptuseraddresscode=? and status in(1,2,4)";
        return getEntryList(sql,new DeliveryOrderRowMapper(),new Object[]{carid,nextstop});
    }

    @Override
    public List<ZhiSuoCarBoxReturnData> getListZhiSuoCarBoxTypeInfoByCarId(String carid) {
        //String sql="select any_value(name) as name,any_value(boxcontent) as boxcontent,count(1) as count from zhisuo_car_box where carid=? and isempty=0  and isorder=0 and status=1 group by boxcontent order by boxcontent asc";
        String sql="select  name,boxcontent,count(1) as count from zhisuo_car_box where carid=? and isempty=0  and isorder=0 and status=1 group by boxcontent order by boxcontent asc";

        return getEntryList(sql,new ZhiSuoCarBoxReturnDataRowMapper(),new Object[]{carid});
    }

    @Override
    public List<ZhiSuoCarBoxReturnData> getListZhiSuoCarBoxInfoByCarId(String carid) {
        //正式
        //String sql="select any_value(name) as name,any_value(boxcontent) as boxcontent,count(1) as count from zhisuo_car_box where carid=? and isempty=0  and isorder=0 and status=1 group by name order by id asc";
        //测试
        String sql="select name,boxcontent,count(1) as count from zhisuo_car_box where carid=? and isempty=0  and isorder=0 and status=1 group by name order by id asc";

        return getEntryList(sql,new ZhiSuoCarBoxReturnDataRowMapper(),new Object[]{carid});
    }

    @Override
    public List<ZhiSuoCarBox> getListZhiSuoCarBoxByContent(String boxcontent,String carid) {
        String sql="select * from zhisuo_car_box where boxcontent=? and carid=? and isempty=0  and isorder=0 and status=1";
        return getEntryList(sql,new ZhiSuoCarBoxRowMapper(),new Object[]{boxcontent,carid});
    }

    @Override
    public void insertNewSellerOrderByUserId(String carid, int boxid, String boxcode, String receiptuseraddressid, String code1, String userid, String receiptusermobile,String receiptcode) {
        String sql="insert into box_delivery_order(carid,boxid,boxcode,create_time,userid,status,receiptusermobile,ordertype,receiptuseraddresscode,receiptuserbuildid,receiptcode,receiptuserorderendtime) values(?,?,?,?,?,?,?,?,?,?,?,date_add(now(), interval 20 second))";
        update(sql,new Object[]{carid,boxid,boxcode,new Date(),userid,0,receiptusermobile,1,code1,receiptuseraddressid,receiptcode});

    }

    @Override
    public List<DeliveryOrder> getListSellerDeliveryOrderByUserIdAndStatus(String userid, String status, Integer page, Integer pagesize) {
        //0待派件  3已签收   4派送中
        String sql="";
        if(status.equals("0")){
            sql= "select * from box_delivery_order where userid=? and status in(0,1,2) and ordertype=1  order by create_time desc limit ?,?";
        }else if(status.equals("3")){
            sql= "select * from box_delivery_order where userid=? and status in(3) and ordertype=1 order by create_time desc limit ?,?";
        }else{
            sql= "select * from box_delivery_order where userid=? and status in(4) and ordertype=1 order by create_time desc limit ?,?";
        }

        return getEntryList(sql,new DeliveryOrderRowMapper(),new Object[]{userid, (page - 1) * pagesize, pagesize});
    }

    @Override
    public List<DeliveryOrder> getListNoDeliveryOrderByUserId(String userid) {
        String  sql= "select * from box_delivery_order where userid=? and status in(0,1,2) and ordertype=1  order by create_time desc ";
        return getEntryList(sql,new DeliveryOrderRowMapper(),new Object[]{userid});
    }

    @Override
    public void updateDeliveryOrderStatusByOrderId(int id,int status) {
        String sql="update box_delivery_order set status=?,isovertime=0 where id=?";
        update(sql,new Object[]{status,id});
    }

    @Override
    public void updateDelOrderStatusByOrderId(String orderid) {
        String sql="update box_delivery_order set status=-1 where id=?";
        update(sql,new Object[]{orderid});
    }

    @Override
    public void updateDelCarBoxIsEmpty(String boxcode, String carid) {
        String sql="update zhisuo_car_box set isempty=0,isorder=0 where name=? and carid=?";
        update(sql,new Object[]{boxcode,carid});

    }

    @Override
    public List<String> getListHasOrderByCarId(int id) {
        String sql="select id from box_delivery_order where ((ordertype=1 and status=4) or(ordertype=0 and (status=1 or(status=2 and (now()>=receiptuserorderstarttime and now()<=receiptuserorderendtime))))) and carid=?";
        return queryStringArray(sql,new Object[]{id});
    }

    @Override
    public void deleteStopBefore(String carid) {
        String sql="delete from zhisuo_car_beforestop where carid=?";
        update(sql,new Object[]{carid});
    }

    @Override
    public String getStopStatusByIdAndStop(int carid, String stop) {
        String sql="select status from  zhisuo_car_nextstoplist  where carid=? and nextstop=?";
        return queryString(sql,new Object[]{carid,stop});
    }

    @Override
    public String getHasPaymentCodeByPaymentCode(String paymentcode) {
        String sql="select id from box_delivery_order where paymentcode=?";
        return queryString(sql,new Object[]{paymentcode});
    }

    @Override
    public List<SendOrder> getListNextSendOrderByNextStop(String nextstop, String carid) {
        String sql="select * from box_usersend_order where carid=? and senduseraddresscode=? and status=0";
        return getEntryList(sql,new SendOrderRowMapper(),new Object[]{carid,nextstop});
    }

    @Override
    public Salesman getSalemanByUserId(Integer userid) {
        String sql="select * from zhisuo_user_salesman where id=? ";
        return getEntry(sql,new SalesmanRowMapper(),new Object[]{userid});
    }

    @Override
    public Salesman getSellermanByUserId(int salemansid) {
        String sql="select (select companyname from sellers_company where id=companyid )as companyname, id,name,mobile from zhisuo_user_sellersman where id=? ";
        return getEntry(sql,new SalesmanRowMapper(),new Object[]{salemansid});
    }

    @Override
    public String getHasOrderByUserIdAndCarIdAndreceiptuserbuildid(String mobile, String carid) {
        String sql="select receiptuserbuildid from box_delivery_order where receiptusermobile=? and carid=?";
        return queryString(sql,new Object[]{mobile,carid});
    }

    @Override
    public void updateCarStatusByCaridAndAddressCode(int id, String stop) {
        String sql="update box_rubbish_order set status=2 and receiptuseraddresscode=? and carid=?";
        update(sql,new Object[]{stop,id});
    }

    @Override
    public void updateRubbishOrderByOrderId(int id) {
        String sql="update box_rubbish_order set status=1 and id=?";
        update(sql,new Object[]{id});
    }
}
